var util = require('../../utils/util.js');
var app = getApp();
var noMore=false,
    isLoading=false,
    page=1,
    limit=20;
Page({
  data: {
      uid:0,
      messages:[],
      sysmsg:{}
  },
  loadMore:function(){
      if(!noMore && !isLoading){
        isLoading = true;
        page ++;
        this.load();
      }
  },
  onLoad: function(data){
    var uid = data.uid || "";
    this.setData({
      uid:uid
    });
    this.load();
  },
  load:function(){
    var that = this;
    util.api({
      data: {
          "uid": this.data.uid,
          "publickey": util.publiccode,
          "ac": "messagelist",
          page:page,
          num:limit
      },
      success: function(responseData){
        isLoading = false;
        var data = that.data.messages;
        var message = [];
        var time;
        if(responseData.data.msg){
          noMore = true;
        }else{
          message = responseData.data.message;
          for(var i=0 ; i<message.length ; i++ ){
            time = new Date(message[i].time*1000);
            message[i].formatTime = time.getFullYear()+"-"+
              ((((time.getMonth() + 1) + "").length == 1) ? ("0" + (time.getMonth() + 1)) : time.getMonth())+"-"+
              ((((time.getDate() + 1) + "").length == 1) ? ("0" + (time.getDate() + 1)) : time.getDate());
          }
          data = data.concat(message);
          that.setData({
            sysmsg: responseData.data.sysmsg,
            messages: data
          });
        }
      }
    });
  },
  goAtherPage: function(e){
      var url = e.currentTarget.dataset.link;
      wx.navigateTo({url: url})
  }
})